
package com.wzapp.sitposture.common.converter;

import com.wzapp.sitposture.common.util.ToolUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.persistence.AttributeConverter;
import java.util.Date;

/**
 * 功能: 将对象属性转换为JSON字符串，保存数据库；或者从数据库读取json转为对象. <br/>
 * 日期: 2017年12月19日 下午3:07:02 <br/>
 * 
 * @author ql
 * @version
 * @since JDK 1.8
 */
public class DefaultDateConverterJson implements AttributeConverter<Long, Date> {

	private static final Logger log = LoggerFactory.getLogger(DefaultDateConverterJson.class);

	private final static ObjectMapper objectMapper = new ObjectMapper();

	@Override
	public Date convertToDatabaseColumn(Long meta) {
		if (ToolUtil.isEmpty(meta) || meta.longValue() == 0){
			return null;
		}

		return new Date(meta);
	}

	@Override
	public Long convertToEntityAttribute(Date dbData) {
        if (ToolUtil.isEmpty(dbData)){
            return null;
        }

		return dbData.getTime();
	}

}